<?php
namespace app\common\model;

use app\common\cache\CacheData;
class Option extends Base
{
	/**
	 * 缓存数据
	 * @return [type] [description]
	 */
	public function cacheData(){
		$data = $this -> select();
		if($data){
			foreach($data as &$value):
				$value = $value->toArray();
			endforeach;
		}
		return $data;
	}

    /**
     * 读取所有option缓存
     * @return [type] [description]
     */
    public static function getAll(){
    	$CACHE = CacheData::getInstance('Option');
        $cacheData = $CACHE->readCache();
        $data = array_combine(array_column($cacheData, 'option_name'), $cacheData);
        foreach($data as &$value):
            $value = $value['option_value'];
        endforeach;
        return $data;  
    }

    /**
     * 读取单个option缓存
     * @param  [type] $option_name [description]
     * @return [type]              [description]
     */
    public static function getOne($option_name){
        $data = self::getAll();
        if(isset($data[$option_name]))
            return $data[$option_name];
        else
            return false;   
    }

    /**
     * 增加配置项
     * @param [type] $option_name  [description]
     * @param [type] $option_value [description]
     */
    public function addOneData($option_name,$option_value){
        if($this->isExist($option_name)){
            $this->where('option_name',$option_name)->update(['option_value'=>$option_value]);
        }else{
            $this->save(['option_name'=>$option_name,'option_value'=>$option_value]);
        }
    }

    /**
     * 配置项是否存在
     * @param  [type]  $option_name [description]
     * @return boolean        [description]
     */
    public function isExist($option_name){
        $res = $this->where('option_name',$option_name)->find();
        if($res)
            return true;
        else
            return false;
    }

}
